package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.Wallet;
import co.yixiang.modules.system.service.WalletService;
import co.yixiang.modules.system.dto.queryCriteria.WalletQueryCriteria;
import co.yixiang.modules.system.dto.WalletDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author db
* @date 2020-11-13
*/
@AllArgsConstructor
@Api(tags = "绑定卡信息管理")
@RestController
@RequestMapping("/api/wallet")
public class WalletController {

    private final WalletService walletService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','wallet:list')")
    public void download(HttpServletResponse response, WalletQueryCriteria criteria) throws IOException {
        walletService.download(Convert.toList(WalletDto.class, walletService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询绑定卡信息")
    @ApiOperation("查询绑定卡信息")
    @PreAuthorize("@el.check('admin','wallet:list')")
    public ResponseEntity<Object> getWallets(WalletQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(walletService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增绑定卡信息")
    @ApiOperation("新增绑定卡信息")
    @PreAuthorize("@el.check('admin','wallet:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Wallet resources){
        return new ResponseEntity<>(walletService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改绑定卡信息")
    @ApiOperation("修改绑定卡信息")
    @PreAuthorize("@el.check('admin','wallet:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Wallet resources){
        walletService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除绑定卡信息")
    @ApiOperation("删除绑定卡信息")
    @PreAuthorize("@el.check('admin','wallet:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        walletService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}